import { View} from '@tarojs/components';
import { ChatMessage, ConservationDetail } from "@/types/conservationType";
import ChatContainer from "@/components/chatEmulator/chatContainer";
import ChatInput from "@/components/chatEmulator/chatInput";
import {Button} from "@taroify/core";
import {useConservationSelector, useDispatchConservation} from "@/hooks/conservation";

export default function Index(props: { conservation: ConservationDetail, onSend: (message: ChatMessage) => void, onStop: () => void }) {
  const { conservation, onSend, onStop} = props;
  const dispatchConservation = useDispatchConservation();
  const generating = useConservationSelector(state => state.conservation.generating);
  const handleConversationClean = () =>{
      dispatchConservation.setConservationPartialInfo({chat_history: [], long_memory: '', short_memory: ''});
      dispatchConservation.updateConservationInfo(conservation.uuid, {chat_history: [], long_memory: '', short_memory: ''})
  }

  return (
    <View style={{ height: '100%', width: '100%', display: 'flex', flexDirection: 'column'}}>
      {/* Chat Container - This will take up the remaining space */}
      <View style={{ flexGrow: 1, overflowY: 'auto', width: '100%'}}>
        <View style={{padding:'20rpx'}}>
          <ChatContainer generating={generating} agent={conservation.agent} chat_messages={conservation.chat_history} note_collection_uuid={conservation.collection_uuid} />
        </View>
      </View>
      <View style={{margin: '5rpx 0 0 20rpx'}}>
        {conservation.chat_history.length !== 0 && <Button disabled={generating} onClick={handleConversationClean} shape='round'>清空上下文</Button>}
      </View>
      {/* Chat Input - Always at the bottom */}
      <View style={{padding: '20rpx'}}>
        <ChatInput onSend={onSend} generating={generating} onStop={onStop} />
      </View>
    </View>
  );
};
